package m.store.action;

import m.base.action.BaseAction;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.action.VueResult;
import m.common.model.util.*;
import m.html.VueFormResult;
import m.html.form.FormButton;
import m.html.form.FormEvent;
import m.html.form.FormInput;
import m.html.form.ModelForm;
import m.store.model.StoreType;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.JSONMessage;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;
import manage.util.DictionaryUtil;

import java.sql.SQLException;

@ActionMeta(name = "store/storeType")
public class StoreTypeAction extends BaseAction {
    private StoreType model;
    public JSONMessage getAllType() throws Exception {
        return new JSONMessage(0)
            .push("list",
                ModelQueryList.getModelList(StoreType.class,new String[]{"*"},null,
                    QueryCondition.eq("store.oid",PowerUtil.getSessionStoreOid()),
                    QueryOrder.asc("sort")
                )
            );
    }
    @RequestMeta(logTitle = "商品分类",logDescription = "商户保存商品分类")
    public JSONMessage doSave() throws Exception {
        PowerUtil.verifyPower(PowerUtil.Store);//权限判断
        if(StringUtil.isSpace(model.getName())) throw new MException(this.getClass(),"名称不能为空");
        model.setStore(PowerUtil.getSessionStore(true));
        ModelCheckUtil.checkUniqueCombine(model,new String[]{"store.oid","name"},"名称重复");
        String msg;
        if(ObjectUtil.isEmptyOid(model)){
            model.setOid(GenerateID.generatePrimaryKey());
            ModelUpdateUtil.insertModel(model);
            msg="保存成功";
        }else{
            ModelUpdateUtil.updateModel(model);
            msg="修改成功";
        }
        DictionaryUtil.clear(StoreType.class,PowerUtil.getSessionStoreOid());
        return new JSONMessage(0,msg);
    }
    /*
     * 编辑商品分类
     */
    public VueResult toEdit() throws Exception {
        PowerUtil.verifyPower(PowerUtil.Store);//权限判断
        if(null!=model&&!StringUtil.isSpace(model.getOid())){
            model= ModelQueryList.getModel(model,"*");
        }else{
            model=new StoreType();
        }
        ModelForm form = new ModelForm("商品分类");
        form.addItem(
            FormInput.input("名称", "model.name").setSpan(3).setRequired(true).setPlaceholder("请输入名称"),
            FormInput.number("排序", "model.sort","",0).setSpan(3).setPlaceholder("请输入排序")
        )
        .addData("model",model)
        .addButton(
            FormButton.pri("保存","doSave")
        )
        .addMethod(
            FormEvent.backFlag,
            FormEvent.doJson("doSave",actionUrl("doSave"),"{model:this.model}")
                .success("backFlag")
        );
        VueFormResult result=new VueFormResult();
        return result.addForm(form);
    }
    public StoreType getModel() {
        return model;
    }

    public void setModel(StoreType model) {
        this.model = model;
    }
}
